﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using Maticsoft.DBUtility;
using ICSharpCode.TextEditor;
using ICSharpCode.TextEditor.Gui.CompletionWindow;
using ICSharpCode.TextEditor.Document;
using ICSharpCode.TextEditor.Actions;
using System.IO;
using TuKu.Common;
using System.Threading;
using TuKu.Load;

namespace TuKu.PPS
{
    public partial class ppsTemplate : Form
    {
        public ppsTemplate()
        {
            InitializeComponent();

        }
        private void init()
        {

            DataTable dt = new BLL.urltemplate().GetList(" 1=1 order by tsort").Tables[0];
            cmbtemplatelist.DataSource = dt;
            cmbtemplatelist.DisplayMember = "tname";
            cmbtemplatelist.ValueMember = "id";
            cmbtemplatelist.SelectedIndexChanged += new EventHandler(cmbtemplatelist_SelectedIndexChanged);
            cmbtemplatelist.SelectedIndex = 0;
            txtSource.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy("HTML");
        }

        void cmbtemplatelist_SelectedIndexChanged(object sender, EventArgs e)
        {
            int id = int.Parse(cmbtemplatelist.SelectedValue.ToString());
            BLL.urltemplate bll = new TuKu.BLL.urltemplate();
            Model.urltemplate model = bll.GetModel(id);
            txtSource.Text = model.tcontent;
        }
        private void ppsTemplate_Load(object sender, EventArgs e)
        {
            init();
        }
        private string contentload(int id)
        {
            Model.urltemplate model = new BLL.urltemplate().GetModel(id);
            return model.tcontent;
        }
        private void btnupdate_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认更新“" + cmbtemplatelist.Text.ToString() + "”?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                int id = int.Parse(cmbtemplatelist.SelectedValue.ToString());
                BLL.urltemplate bll = new TuKu.BLL.urltemplate();
                Model.urltemplate model = bll.GetModel(id);
                model.tcontent = txtSource.Text;
                bll.Update(model);
                MessageBox.Show("更新完成");
                init();
            }
        }

        private void btnload_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认更新网站首页?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {

                string webpath = Common.PathClass.webpath + "\\PPS";
                if (!Directory.Exists(webpath))
                {
                    Directory.CreateDirectory(webpath);
                }
                string fileName = webpath + "\\index.html";
                FileManager.WriteFile(fileName, txtSource.Text, false);
                MessageBox.Show("生成首页");
                Model.Ftp model = new TuKu.Model.Ftp();
                model.password = System.Configuration.ConfigurationManager.AppSettings["password"];
                model.port = int.Parse(System.Configuration.ConfigurationManager.AppSettings["port"]);
                model.server = System.Configuration.ConfigurationManager.AppSettings["server"];
                model.username = System.Configuration.ConfigurationManager.AppSettings["username"];
                Common.FTP ftp = new FTP();

                ftp.Connect(model.server, model.port, model.username, model.password);
                while (!ftp.IsConnected)
                {
                    Thread.Sleep(10);
                }
                if (!ftp.OpenUpload(fileName, "index.html"))
                {
                    ftp.Disconnect();
                }
                int perc = 0;
                //开始进行上传

                LoadingHandler.Show(this, LoadingStyle.Progress, args =>
                {
                    args.MaxValue = 100;
                    while (ftp.DoUpload() > 0)
                    {
                        args.Execute(ex =>
                       {
                           perc = (int)(((ftp.BytesTotal) * 100) / ftp.FileSize);
                       });
                        args.Value = perc;
                        args.Change();
                        System.Threading.Thread.Sleep(5);

                    }

                });
                ftp.Disconnect();
            }
            MessageBox.Show("上传完毕");
        }

        private void tabTemplate_Selected(object sender, TabControlEventArgs e)
        {
            webBrowser1.DocumentText = txtSource.Text;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string str = "http://tu.pps.tv/pics/56198/2.html";
            string newstr = str.Substring(22, 5);
            newstr.showBox();
        }


    }
}
